{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名 占晓洋 \n",
    "+ 学号 0165222"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "我的眼睛望着窗外\n",
      "幻想如何对你表白\n",
      "心事写满脸庞\n",
      "已经不用去猜\n",
      "未来还有多远才来\n",
      "爱已变为等待\n",
      "错过这一场\n",
      "我将会独自消失于人海\n",
      "\n",
      "\n",
      "我的眼睛望着窗外\n",
      "幻想如何对你表白\n",
      "心事写满脸庞\n",
      "已经不用去猜\n",
      "\n",
      "\n",
      "\n",
      "我的眼睛望着窗外\n",
      "幻想如何对你表白\n",
      "心事写满脸庞\n",
      "已经不用去猜\n",
      "\n",
      "\n",
      "\n",
      "我的眼睛望着窗外\n",
      "幻想如何对你表白\n",
      "心事写满脸庞\n",
      "已经不用去猜\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('''\n",
    "我的眼睛望着窗外\n",
    "幻想如何对你表白\n",
    "心事写满脸庞\n",
    "已经不用去猜\n",
    "未来还有多远才来\n",
    "爱已变为等待\n",
    "错过这一场\n",
    "我将会独自消失于人海\n",
    "''')\n",
    "def  repeat():\n",
    "    print('''\n",
    "我的眼睛望着窗外\n",
    "幻想如何对你表白\n",
    "心事写满脸庞\n",
    "已经不用去猜\n",
    "\n",
    "''')\n",
    "repeat()\n",
    "repeat()\n",
    "repeat()\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值=14.000000 \n",
      "方差=185.555556 \n",
      "n=10\n",
      "\n",
      "均值=19.166667 \n",
      "方差=624.878788 \n",
      "n=12\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def hanshu(*arg):\n",
    "        sum=0\n",
    "        n=len(arg)\n",
    "        for i in arg:\n",
    "            sum+= float(i)\n",
    "        mean=sum/n\n",
    "        ss=0\n",
    "        for i in arg:\n",
    "            ss=ss+(i-mean)**2\n",
    "            variance=ss/(n-1)\n",
    "        print('均值=%f' % mean,\n",
    "               '\\n方差=%f'  % variance,\n",
    "                '\\nn=%d\\n' %n)\n",
    "hanshu(9,7,44,1,7,4,8,17,32,11)\n",
    "hanshu(11,9,22,18,19,4,17,14,12,96,7,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择输出标准差还是方差，1代表方差\n",
      "1\n",
      "n=10 \n",
      "均值=14.000000\n",
      "方差=185.555556\n",
      "\n",
      "选择输出标准差还是方差，1代表方差\n",
      "2\n",
      "n=12 \n",
      "均值=19.166667\n",
      "标准差=24.997576\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def hanshu(*arg,mean=0, variance=0):\n",
    "        sum=0\n",
    "        n=len(arg)\n",
    "        for i in arg:\n",
    "            sum+= float(i)\n",
    "        mean=sum/n\n",
    "        ss=0\n",
    "        for i in arg:\n",
    "            ss=ss+(i-mean)**2\n",
    "            variance=ss/(n-1)\n",
    "        print('选择输出标准差还是方差，1代表方差')\n",
    "        choice=int(input())\n",
    "        print('n=%d' %n,\n",
    "               '\\n均值=%f' % mean )\n",
    "        if choice==1:\n",
    "                print('方差=%f\\n'  % variance)\n",
    "        else:\n",
    "             print('标准差=%f\\n'  % variance**0.5) \n",
    "hanshu(9,7,44,1,7,4,8,17,32,11)\n",
    "hanshu(11,9,22,18,19,4,17,14,12,96,7,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请选择输出标准差还是方差，1代表方差\n",
      "1\n",
      "请选择输出均值的标准误、最大值、最小值或是不输出，1代表输出标准误、最大值、最小值\n",
      "1\n",
      "n=10 \n",
      "均值=14.000000\n",
      "方差=185.555556\n",
      "\n",
      "标准误=4.307616 最大值=44.000000 \n",
      "最小值=1.000000\n",
      "\n",
      "请选择输出标准差还是方差，1代表方差\n",
      "2\n",
      "请选择输出均值的标准误、最大值、最小值或是不输出，1代表输出标准误、最大值、最小值\n",
      "1\n",
      "n=12 \n",
      "均值=19.166667\n",
      "标准差=24.997576\n",
      "\n",
      "标准误=7.216179 最大值=96.000000 \n",
      "最小值=1.000000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def hanshu(*arg):\n",
    "        sum=0\n",
    "        n=len(arg)\n",
    "        for i in arg:\n",
    "            sum+= float(i)\n",
    "        mean=sum/n\n",
    "        ss=0\n",
    "        for i in arg:\n",
    "            ss=ss+(i-mean)**2\n",
    "            variance=ss/(n-1)\n",
    "            standard_error=variance**0.5/n**0.5\n",
    "        print('请选择输出标准差还是方差，1代表方差')\n",
    "        choice1=int(input())\n",
    "        print('请选择输出均值的标准误、最大值、最小值或是不输出，1代表输出标准误、最大值、最小值')\n",
    "        choice2=int(input())\n",
    "        print('n=%d' %n,\n",
    "               '\\n均值=%f' % mean )\n",
    "        if choice1==1:\n",
    "                print('方差=%f\\n'  % variance)\n",
    "        else:\n",
    "                print('标准差=%f\\n'  % variance**0.5) \n",
    "        if choice2==1:\n",
    "             print('标准误=%f'  % standard_error,\n",
    "                     '最大值=%f' % max(arg),     \n",
    "                     '\\n最小值=%f\\n' % min(arg)) \n",
    "hanshu(9,7,44,1,7,4,8,17,32,11)\n",
    "hanshu(11,9,22,18,19,4,17,14,12,96,7,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
